﻿Imports System.Text
Imports System.Data
Imports Microsoft.Office.Interop
Imports Global_Visas_Business_Layer 

Partial Public Class frmAddEvent

    Private _EventType As TasksEvents.EventTypes
    Public LinkId As Long
    'Public LinkType As TasksEvents.LinkTypes


    Public Property EventType() As TasksEvents.EventTypes
        Get
            Return _EventType
        End Get
        Set(ByVal value As TasksEvents.EventTypes)
            _EventType = value

            Select Case value
                Case TasksEvents.EventTypes.EventPerson, TasksEvents.EventTypes.EventCompany, TasksEvents.EventTypes.EventCase
                    Me.radCatSales.IsChecked = True
                Case TasksEvents.EventTypes.EventUserTask
                    Me.radCatAll.IsChecked = True
                    Me.radTask.IsChecked = True
                    Me.txtTaskDate.Text = Format(Now, "dd-MMM-yyyy")
                    Me.grpEventTypes.IsEnabled = False
                    Me.grpCategory.IsEnabled = False

            End Select
        End Set
    End Property

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub frmAddEvent_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Cancel Or e.Key = Key.Escape Then
            btnCancel_Click(sender, e)
        End If
    End Sub

    Private Sub frmAddEvent_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        grpTask.Visibility = Windows.Visibility.Hidden
        If Me.LinkId = 0 And Me._EventType <> Global_Visas_Business_Layer.TasksEvents.EventTypes.EventUserTask Then
            CommonFuncs.DisplayMessage("Link ID not set, contact developer!", CommonDialog.CustomDialogIcons.Information, "Add Event")
            Me.Close()
            Exit Sub
        End If

        If Me._EventType = Global_Visas_Business_Layer.TasksEvents.EventTypes.NotSet Then
            CommonFuncs.DisplayMessage("Link Type not set, contact developer!", CommonDialog.CustomDialogIcons.Information, "Add Event")
            Me.Close()
            Exit Sub
        End If

        Select Case Me.EventType
            Case Global_Visas_Business_Layer.TasksEvents.EventTypes.EventPerson
                Me.lblAddTo.Content = Global_Visas_Business_Layer.GlobalCRM.CurrentCandidateName & " (" & Global_Visas_Business_Layer.GlobalCRM.CurrentCandidateID & ")"

            Case Global_Visas_Business_Layer.TasksEvents.EventTypes.EventUserTask
                Me.lblAddTo.Content = Global_Visas_Business_Layer.CommonDataFunctions.GetConsultantName(Global_Visas_Business_Layer.GlobalCRM.CurrentConID)

            Case Global_Visas_Business_Layer.TasksEvents.EventTypes.EventCompany
                Me.lblAddTo.Content = Global_Visas_Business_Layer.Company.GetCompany(Global_Visas_Business_Layer.GlobalCRM.CurrentCompanyID).CompanyName

            Case Global_Visas_Business_Layer.TasksEvents.EventTypes.EventCase
                Me.lblAddTo.Content = "Case: " & Global_Visas_Business_Layer.GlobalCRM.CurrentCaseID
        End Select


        Me.DisplayEventTypes()

        With Me.drpCon
            .SelectedValuePath = "con_id"
            .DisplayMemberPath = "con_name"
        End With
        Dim itemsConsultantlist As DataTable = Global_Visas_Business_Layer.CommonDataFunctions.ConsultantList
        DataBinding.BindComboBox(itemsConsultantlist, drpCon)

        Me.drpCon.SelectedValue = Global_Visas_Business_Layer.GlobalCRM.CurrentConID

    End Sub

    Private Sub DisplayEventTypes()

        Dim SQL As StringBuilder
        Dim EventGroup, StageId As Integer
        Dim CustType As String
        Dim myData As DataTable = Nothing
        Try

        
            Select Case Me.EventType

                Case 1
                    'Case Event
                    EventGroup = CommonDataFunctions.GblFunc_Get_Dataset(GlobalCRM.CurrentCaseID, 5)

                    If EventGroup = 0 Then
                        EventGroup = 2
                    End If

                    SQL = New StringBuilder

                    'Get Stage
                    With SQL
                        .Append("Select Case_StatusID from D_case where case_ID = " & Global_Visas_Business_Layer.GlobalCRM.CurrentCaseID)
                    End With

                    StageId = CommonDataFunctions.GetSQLScalar(SQL.ToString)

                Case 2
                    'Person Event
                    EventGroup = 4
                    StageId = 0

                Case 3
                    'Company event
                    EventGroup = 3
                    StageId = 0

                Case 4
                    'User Tasks (Unlinked)
                    EventGroup = 8
                    StageId = 0

            End Select

            If Global_Visas_Business_Layer.GlobalCRM.CurrentCompanyID > 0 Then
                CustType = "(0,2)"
            Else
                CustType = "(0,1)"
            End If

            SQL = New StringBuilder

            With SQL
                .Append("Select EventType_Name")
                .Append(" FROM L_EventTypes INNER JOIN")
                .Append(" I_EventLinks ON L_EventTypes.EventType_ID = I_EventLinks.EventLink_EventTypeID")
                .Append(" WHERE EventLink_EventGroupID = " & EventGroup)
                .Append(" AND EventType_CustomerType in " & CustType)
                If StageId <> 0 Then
                    'Limit by current case stage
                    .Append("AND EventLink_StageStart <= " & StageId)
                    .Append(" AND (EventLink_StageEnd >= " & StageId & " OR EventLink_StageEnd = 0)")
                End If

                If Me.radCatSales.IsChecked Then
                    .Append(" AND EventType_Scope in (1,3)")
                ElseIf Me.radCatCW.IsChecked Then
                    .Append(" AND EventType_Scope in (2,3)")
                ElseIf Me.radCatAll.IsChecked Then
                    'All
                End If


                If Security.CheckSecurityAccess(35) = 0 Then
                    If Security.CheckSecurityAccess(34) = 1 Then
                        'Supervisor and normal events
                        .Append(" AND EventLink_SecAccess < 2")
                    Else
                        'Normal events only
                        .Append(" AND EventLink_SecAccess = 0")
                    End If
                Else
                    'Full access, return them all.
                End If

                .Append(" ORDER BY EventLink_Order")

            End With

            myData = CommonDataFunctions.GetSQLDatatable(SQL.ToString)
            DataBinding.BindListView(myData, lstEventTypes)
            'Me.lstEventTypes.DisplayMemberPath = "EventType_Name"
            'Me.lstEventTypes.ItemsSource = myData.Rows.ToQueryable

            ' select top item in list (because otherwise it looks selected, but isn't really)
            Me.lstEventTypes.SelectedIndex = 0

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "DisplayEventTypes", True)
        Finally
            SQL = Nothing
            myData.Dispose()
        End Try
    End Sub

    Private Sub radCatAll_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radCatAll.Checked
        DisplayEventTypes()
    End Sub

    Private Sub radCatCW_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radCatCW.Checked
        DisplayEventTypes()
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        Dim IsUrgent As Boolean

        If Me.lstEventTypes.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Please select an Event Category", CommonDialog.CustomDialogIcons.Information, "Save")
            Exit Sub
        End If
        If Me.txtEvent.Text.Trim = String.Empty Then
            'MsgBox("Please enter some Notes", MsgBoxStyle.Exclamation, "Notes")
            'Me.txtEvent.Focus()
            'Exit Sub
        End If
        If Me.radEvent.IsChecked Then
            If MsgBox("Would you like to book a task as well?", vbYesNo, "Task?") = MsgBoxResult.Yes Then
                Dim currentItem As DataRowView = CType(lstEventTypes.SelectedItem, DataRowView)
                Dim taskEvents As New Global_Visas_Business_Layer.TasksEvents()
                taskEvents.GblFunc_Add_Event(currentItem(0), Me.txtEvent.Text, Global_Visas_Business_Layer.TasksEvents.EventStatus.SingleEvent, Me.LinkId, Me._EventType, , , , , 1)
                Me.radTask.IsChecked = True
                Me.drpCon.Focus()
                txtEvent.Text = String.Empty
                Exit Sub
                'TasksEvents.GblFunc_Add_Event(Me.lstEventTypes.Text, Me.txtEvent.Text, TasksEvents.EventStatus.Task, Me.LinkId, Me._EventType, IsUrgent, 0, Me.drpCon.SelectedValue, Format(Now, "dd-MMM-yyyy"), 1)
            Else
                Dim taskEvents As New Global_Visas_Business_Layer.TasksEvents()
                Dim currentItem As DataRowView = CType(lstEventTypes.SelectedItem, DataRowView)
                taskEvents.GblFunc_Add_Event(currentItem(0), Me.txtEvent.Text, Global_Visas_Business_Layer.TasksEvents.EventStatus.SingleEvent, Me.LinkId, Me._EventType, , , , , 1)
            End If
        Else

            If Not IsDate(Me.txtTaskDate.Text) Then
                CommonFuncs.DisplayMessage("Not a valid date", CommonDialog.CustomDialogIcons.Information, "Date")
                Me.txtTaskDate.Focus()
                Exit Sub
            End If

            If CDate(Me.txtTaskDate.Text) < (New Global_Visas_Business_Layer.CommonFuncs()).FormatDate(Now) Then
                CommonFuncs.DisplayMessage("Cannot book a task in the past", CommonDialog.CustomDialogIcons.Information, "Date Validation")
                Exit Sub
            End If
            If Me.chkUrgent.IsChecked Then
                IsUrgent = True
            End If

            ' add event
            Dim currentItem As DataRowView = CType(lstEventTypes.SelectedItem, DataRowView)
            Dim taskEvents As New Global_Visas_Business_Layer.TasksEvents()
            taskEvents.GblFunc_Add_Event(currentItem(0), Me.txtEvent.Text, Global_Visas_Business_Layer.TasksEvents.EventStatus.Task, Me.LinkId, Me._EventType, IsUrgent, 0, Me.drpCon.SelectedValue, Me.txtTaskDate.Text, 1)

        End If

        ' I think it is pointless to ask this, as they have the option to do this anyway and have obviously chosen not to
        'If Me.radEvent.Checked Then
        '    If MsgBox("Do you wish to book a task as well?", vbYesNo, "Book Task") = MsgBoxResult.Yes Then
        '        Me.radTask.Checked = True
        '        Exit Sub
        '    End If
        'End If

        Me.Close()

    End Sub

    Private Sub btnCheckDay_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCheckDay.Click
        Dim BrowseTasks As New frmBrowseTasks()
        BrowseTasks.txtTaskDate.Text = Me.txtTaskDate.Text
        BrowseTasks.ShowDialog()
    End Sub

    Private Sub radTask_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radTask.Checked
        Me.txtTaskDate.Text = Format(Now, "dd-MMM-yyyy")
        If Me.radTask.IsChecked = True Then
            Me.grpTask.Visibility = Windows.Visibility.Visible
        Else
            Me.grpTask.Visibility = Windows.Visibility.Hidden
        End If


    End Sub

    Private Function GetPopUpCalendarDate(Optional ByVal strCurrentDate As String = "") As String
        ' get date selected in calendar pop up (or return their current date if they click Cancel)
        Dim frmObject As New frmCalendarPopUp()
        frmObject.ShowDialog()

        If Format(GlobalCRM.DatePopUpDate, "dd-MMM-yyyy") = "01-Jan-0001" Then
            GetPopUpCalendarDate = strCurrentDate
        Else
            GetPopUpCalendarDate = Format(GlobalCRM.DatePopUpDate, "dd-MMM-yyyy")
        End If

    End Function

    Private Sub btnCalendar_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        Me.txtTaskDate.Text = GetPopUpCalendarDate(Me.txtTaskDate.Text)
        'frmCalendarPopUp.ShowDialog()
        'Me.txtTaskDate.Text = Format(GlobalCRM.DatePopUpDate, "dd-MMM-yyyy")

    End Sub

    Private Sub radEvent_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radEvent.Checked
        If Not grpTask Is Nothing Then
            If radEvent.IsChecked = True Then
                grpTask.Visibility = Windows.Visibility.Hidden
            Else
                grpTask.Visibility = Windows.Visibility.Visible
            End If
        End If
    End Sub

    Private Sub txtTaskDate_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles txtTaskDate.SelectedDateChanged
        Me.txtTaskDate.Text = (New CommonFuncs).FormatDate(txtTaskDate.SelectedDate)
    End Sub
End Class
